package cn.elves.model.auth.service.impl;

import cn.elves.model.auth.entity.po.Role;
import cn.elves.model.auth.entity.po.RoleInterfaceRelation;
import cn.elves.model.auth.entity.po.RoleMenuRelation;
import cn.elves.model.auth.mapper.RoleMapper;
import cn.elves.model.auth.service.RoleInterfaceRelationService;
import cn.elves.model.auth.service.RoleMenuRelationService;
import cn.elves.model.auth.service.RoleService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
* @author pc
* @description 针对表【tb_role】的数据库操作Service实现
* @createDate 2024-05-26 18:34:43
*/
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role>
    implements RoleService{

    @Resource
    private RoleInterfaceRelationService roleInterfaceRelationService;

    @Resource
    private RoleMenuRelationService roleMenuRelationService;

    @Override
    public void allocationMenu(Long roleId, Set<Long> menuIdSet) {
        roleMenuRelationService.remove(new QueryWrapper<RoleMenuRelation>().eq("role_id", roleId));

        List<RoleMenuRelation> roleMenuRelationList = menuIdSet.stream().map(menuId -> {
            RoleMenuRelation roleMenuRelation = new RoleMenuRelation();
            roleMenuRelation.setMenuId(menuId);
            roleMenuRelation.setRoleId(roleId);
            return roleMenuRelation;
        }).collect(Collectors.toList());
        roleMenuRelationService.saveBatch(roleMenuRelationList);
    }

    @Override
    public void allocationInterface(Long roleId, Set<Long> interfaceIdSet) {
        roleInterfaceRelationService.remove(new QueryWrapper<RoleInterfaceRelation>().eq("role_id", roleId));

        List<RoleInterfaceRelation> userRoleRelationList = interfaceIdSet.stream().map(interfaceId -> {
            RoleInterfaceRelation roleInterfaceRelation = new RoleInterfaceRelation();
            roleInterfaceRelation.setInterfaceId(interfaceId);
            roleInterfaceRelation.setRoleId(roleId);
            return roleInterfaceRelation;
        }).collect(Collectors.toList());
        roleInterfaceRelationService.saveBatch(userRoleRelationList);
    }
}




